/**
 * @file util/nmap.c
 * @brief nmap dynamic loadable library
 * @author Bojan Nesovic
 * @date 2013-11-14
*/

#include <stdio.h>
#include <string.h>
#include "nmap.h"

int nmap(char *host, FILE *outputfile) {
  FILE *pptr = NULL;
  char systemcall[1280];
  char buffer[1280];

  snprintf(systemcall, sizeof(systemcall), "nmap %s", host);

  pptr = popen(systemcall, "r");

  if (pptr == NULL)
    return -1;

  printf("%s\n", systemcall);

  /* read lines from process nmap  */
  while (fgets(buffer, 1023, pptr) != NULL) {
    fwrite(buffer, strlen(buffer), 1, outputfile);
  }

  pclose(pptr);

  return 0;
}
